/**
 * Created by Administrator on 2015/10/30.
 */

//分页条插件
$.fn.pageBar=function(){
    var args = arguments;
    var options = args[0];
    //第一个参数如果是字符串,则是方法的调用,其他则是初始化插件
    if(typeof options === 'string'){
    var retVal;
    this.each(function(){
    var jq = $(this);
    var fn =  $.fn.pageBar.methods[options];
    if(typeof fn === 'function'){
    retVal = fn.apply(jq,$.makeArray(args).slice(1) || []);
    if(retVal !== undefined) return false;
    }else{
    throw new Error('没有此方法:'+options);
    }
    });
    return retVal;
    }
    //初始化插件
    options = options || {};
    return this.each(function(){
    var jq = $(this);
    var jumpFnName = 'jumpFnName'+Math.floor(Math.random()*100000000+1);
    window[jumpFnName]=function(i){
    options.onJump.call(jq,i);
    }
    jq.data('jumpFnName',jumpFnName);
    });
    }
$.fn.pageBar.methods = {
    //渲染html内容的方法
    render:function (data){
    var fnName=this.data('jumpFnName');
    if(!fnName) throw new Error('未初始化');
    this.html('');
    var totalPage = parseInt(data.totalCount / data.pageSize) + (data.totalCount % data.pageSize ? 1 : 0);
    if(totalPage===1){
        return;
    }
    var totalCount = data.totalCount;
    var currPage = data.currPage+1;
    if(currPage > totalPage ){
    currPage = 1;
    }
    if(totalCount > 0){
    var pageStart = currPage - 2 > 0 ? currPage - 2 : 1;
    var pageEnd = pageStart + 4 >= totalPage ? totalPage : pageStart + 4;
    this.append('<li '+(currPage == 1 ?'class="disabled"' :'')+'><a '+
    (currPage > 1 ?' onclick="'+fnName+'('+1+')" ' :'')+
    '  href="#" aria-label="Previous"><span aria-hidden="true">首页</span></a></li>');
    this.append('<li '+(currPage == 1 ?'class="disabled"' :'')+'><a '+
    (currPage > 1 ?' onclick="'+fnName+'('+(currPage -1)+')" ' :'')+
    '  href="#" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>');
    if(totalPage>5&&pageEnd-pageStart<4){
        var start= pageEnd-4;
        var end= pageEnd;
        for(var i=start;i<=end;i++){
            if(i == currPage){
                this.append('<li class="active"><a href="#">'+i+'</a></li>');
            }else{
                this.append('<li><a href="#" onclick="'+fnName+'('+i+')">'+i+'</a></li>');
            }
        }

    }else if(totalPage<=5){
        var start= 1;
        var end= pageEnd;
        for(var i=start;i<=end;i++){
            if(i == currPage){
                this.append('<li class="active"><a href="#">'+i+'</a></li>');
            }else{
                this.append('<li><a href="#" onclick="'+fnName+'('+i+')">'+i+'</a></li>');
            }
        }
    }else{
        for(var i=pageStart;i<=pageEnd;i++){
            if(i == currPage){
                this.append('<li class="active"><a href="#">'+i+'</a></li>');
            }else{
                this.append('<li><a href="#" onclick="'+fnName+'('+i+')">'+i+'</a></li>');
            }
        }

    }

    this.append('<li '+(currPage == totalPage ?' class="disabled" ' :'')+'><a href="#"  '+
    (currPage < totalPage ?' onclick="'+fnName+'('+(currPage + 1)+')" ' : '')+
    ' aria-label="Previous"><span aria-hidden="true">&raquo;</span></a></li>');
    this.append('<li '+(currPage == totalPage ?' class="disabled" ' :'')+'><a href="#"  '+
    (currPage < totalPage ?' onclick="'+fnName+'('+totalPage+')" ' : '')+
    ' aria-label="Previous"><span aria-hidden="true">尾页</span></a></li>');
     }
   }
}


// 以上是插件内容----------------------------------------------------------------------------

//插件用法
//function page(){
//    $('#pagelist').pageBar({
//        onJump:function(targetPage){//用户点击跳转时候回调
//            //参数targetPage为用户点击的页码
//            //这里一般向后台请求数据,请求成功后执行下面代码,重新渲染html
//            this.pageBar('render',{//渲染html
//                pageSize:10
//                ,totalCount:totalCount
//                ,currPage:curPage
//            })
//        }
//    }).pageBar('render',{//渲染html
//        pageSize:10  //每页显示数
//        ,totalCount:totalCount//数据总条数
//        ,currPage:curPage   //当前页
//    });
//
//}

